package org.lmib.abt.checkers;
/*
 * Copyright (c) 1996/1997 F. Di Fabio
 * All rights reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 675
 * Mass Ave, Cambridge, MA 02139, USA.
 */


import java.util.Vector;

class Box {
  static Box NOBOX = null;
  static int NW = 0;
  static int NE = 1;
  static int SW = 2;
  static int SE = 3;
  private int piece;
  public int idBox;
  //Collegamenti con le celle adiacenti
  //nel caso di celle che non hanno quattro celle adiacenti
  //c'è il valore NOBOX

  public Box[] near;

public Box(int id) {
  near = new Box[4];
  idBox = id;
}

public Box canJump(int type, int where) {
 
  if(type == CheckersGlobal.MANWHITE) {
    if((piece == CheckersGlobal.MANBLACK) && (near[where].isEmpty())) { 
      return near[where];
    }
  }
  if(type == CheckersGlobal.KINGWHITE) {
    if(near[where].isEmpty() && (this.getColor() == CheckersGlobal.BLACK)) { 
      return near[where];
    }
  }
  if(type == CheckersGlobal.MANBLACK) {
    if((piece == CheckersGlobal.MANWHITE) && (near[where].isEmpty())) {
      return near[where];
    }
  }
  if(type == CheckersGlobal.KINGBLACK) {
    if((near[where].isEmpty()) && (this.getColor() == CheckersGlobal.WHITE)) {
      return near[where];
    }
  }
  return NOBOX;
}

public void findMoves(int color, Vector vtrListOfMoves) {
  Box jumpTo;

  if((this.getColor() == color) && (color == CheckersGlobal.WHITE)) {
    if(piece == CheckersGlobal.MANWHITE) {
      try {
        if(near[NW].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.MANSTEP,this,near[NW]));
        }
        else {
          try {
            jumpTo = near[NW].canJump(CheckersGlobal.MANWHITE,NW);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),NW,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
      try {
        if(near[NE].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.MANSTEP,this,near[NE]));
        }
        else {
          try {
            jumpTo = near[NE].canJump(CheckersGlobal.MANWHITE,NE);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),NE,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
    }
    if(piece == CheckersGlobal.KINGWHITE) {
      //mosse che puo fare solo una dama bianca
      try {	//andare indietro e mangiare qualsiasi pezzo nero
        if(near[SW].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.KINGSTEP,this,near[SW]));
        }
        else {
          try {
            jumpTo = near[SW].canJump(CheckersGlobal.KINGWHITE,SW);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),SW,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }		
      } catch(NullPointerException e) {}
      try {
        if(near[SE].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.KINGSTEP,this,near[SE]));
        }
        else {
          try {
            jumpTo = near[SE].canJump(CheckersGlobal.KINGWHITE,SE);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),SE,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
      try { //andare avanti mangiando pezzi neri
        if(near[NW].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.KINGSTEP,this,near[NW]));
        }
        else {
          try {
            jumpTo = near[NW].canJump(CheckersGlobal.KINGWHITE,NW);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),NW,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
      try {
        if(near[NE].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.KINGSTEP,this,near[NE]));
        }
        else {
          try {
            jumpTo = near[NE].canJump(CheckersGlobal.KINGWHITE,NE);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),NE,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
    }
  }
  //fine mosse bianco
  //inizio mosse nero
  if((this.getColor() == color) && (color == CheckersGlobal.BLACK)) {
    if(piece == CheckersGlobal.MANBLACK) {
      //mosse che puo fare sia una pedina nera che una dama nera
      try {
        if(near[SW].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.MANSTEP,this,near[SW]));
        }
        else {
          try {
            jumpTo = near[SW].canJump(CheckersGlobal.MANBLACK,SW);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),SW,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
      try {
        if(near[SE].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.MANSTEP,this,near[SE]));
        }
        else {
          try {
            jumpTo = near[SE].canJump(CheckersGlobal.MANBLACK,SE);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),SE,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
      //fine mosse che puo fare una pedina nera
    }
    if(piece == CheckersGlobal.KINGBLACK) {
      //mosse che puo fare solo una dama nera
      try {	//andare indietro e mangiare qualsiasi pezzo bianco
        if(near[NW].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.KINGSTEP,this,near[NW]));
        }
        else {
          try {
            jumpTo = near[NW].canJump(CheckersGlobal.KINGBLACK,NW);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),NW,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
      try {
        if(near[NE].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.KINGSTEP,this,near[NE]));
        }
        else {
          try {
            jumpTo = near[NE].canJump(CheckersGlobal.KINGBLACK,NE);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),NE,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
      try { //andare avanti mangiando dama bianche
        if(near[SW].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.KINGSTEP,this,near[SW]));
        }
        else {
          try {
            jumpTo = near[SW].canJump(CheckersGlobal.KINGBLACK,SW);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),SW,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
      try {
        if(near[SE].isEmpty()) { 
          vtrListOfMoves.addElement(new Move(Move.NOJUMP,Move.KINGSTEP,this,near[SE]));
        }
        else {
          try {
            jumpTo = near[SE].canJump(CheckersGlobal.KINGBLACK,SE);
            if(jumpTo != NOBOX) {
              vtrListOfMoves.addElement(new Move(Move.JUMP,jumpTo.getPiece(),SE,this,jumpTo));
            }
          } catch(NullPointerException e) {}
        }
      } catch(NullPointerException e) {}
    }
  }
}

public int getColor() {

  if(piece == CheckersGlobal.MANWHITE || piece == CheckersGlobal.KINGWHITE) {
    return CheckersGlobal.WHITE;
  }
  if(piece == CheckersGlobal.MANBLACK || piece == CheckersGlobal.KINGBLACK) {
    return CheckersGlobal.BLACK;
  }
  return CheckersGlobal.UNDEF;
}

public int getPiece() {

  return piece;
}

public boolean isEmpty() {

  return (piece == CheckersGlobal.EMPTYBLUE);
}

public void setBox(int p, Box nw, Box ne, Box sw, Box se) {

  piece = p;
  near[NW] = nw;
  near[NE] = ne;
  near[SW] = sw;
  near[SE] = se;
}

public void setPiece(int newPiece) {

  piece = newPiece;
}
}
